#include<iostream>
using namespace std;
bool binarychop(int a[], int len,int num,int &Index)
{
	int low = 0;
	int high = len;
	int mid = (high + low) / 2;
	while (1)
	{
		
		if (a[mid] == num)//如果中间值等于定值，记下中间值在数组中的位置，返回true
		{
			Index = mid;
			return true;
		}
		else if (a[mid] < num)//若中间值小于定值，将low移到mid位置，并且更新mid的值
		{
			low = mid;
			mid = (high + low) / 2;
		}
		else if (a[mid] > num)//若中间值大于定值，将high移到mid位置，并且更新mid的值
		{
			high = mid;
			mid = (high + low) / 2;
		}
		else    //数组中没有要寻找的定值，返回false
		{
			return false;
		}
	}
	return true;
}
int main()
{
	int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int Index;
	int len = sizeof(a) / sizeof(int);
	binarychop(a, len,1, Index);
	cout << "所要查找的元素在数组中的位置: " << Index << endl;
	return 0;
}